package com.wjs.juc.Future;

import org.junit.Test;

import java.net.SocketImpl;
import java.util.concurrent.*;

public class FutureTask1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        long startTime=System.currentTimeMillis();
        FutureTask<String> futureTask1 = new FutureTask<>(() -> {
            TimeUnit.MILLISECONDS.sleep(300);
            return "task1 完成";
        });
        FutureTask<String> futureTask2 = new FutureTask<>(() -> {
            TimeUnit.MILLISECONDS.sleep(500);
            return "task2 完成";
        });
        pool.submit(futureTask1);
        pool.submit(futureTask2);
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(futureTask1.get());
        System.out.println(futureTask2.get());
        long end=System.currentTimeMillis();
        System.out.println("完成耗时"+(end-startTime)+"ms");
        pool.shutdown();
    }
}
